#!/bin/sh
#
# Copyright (C) 2006 Lars Strand
#
# Munin-plugin to monitor processes on Linux
# FreeBSD, OpenBSD, NetBSD, Solaris and OSX.
#
# Require munin-server version 1.2.5 or 1.3.3 (or higher).
#
# This plugin is backwards compatible with the old processes-plugins
# found on SunOS, Linux and *BSD (i.e. the history is preserved).
#
# All fields have colours associated with them which reflect the type
# of process (sleeping/idle = blue, running = green,
# stopped/zombie/dead = red, etc.) 
#
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 2 dated June,
# 1991.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
#
# Parameters understood:
#
#	config		(required)
#	autoconf	(optional - used by munin-config)
#
# $Log$
#
#
#
# Magick markers (optional - used by munin-config and som installation
# scripts):
#
#%# family=auto
#%# capabilities=autoconf

# Search for program in $PATH unless predefined.
[ $awk ]     || awk="awk"
[ $ps ]      || ps="ps"

# Find operating system
[ $OPERSYS ] || OPERSYS=`uname` || exit 1

if [ "$1" = "autoconf" ]; then
	echo yes
	exit 0
fi

# Define colours
RUNNABLE='22ff22'         # Green
SLEEPING='0022ff'         # Blue
STOPPED='cc0000'          # Darker red
ZOMBIE='990000'           # Darkest red
UNINTERRUPTIBLE='ffa500'  # Orange
IDLE='4169e1'             # Royal blue
PAGING='00aaaa'           # Darker turquoise
INTERRUPT='ff00ff'        # Fuchsia
LOCK='ff3333'             # Lighter red
RUNNING='00ff7f'          # Spring green
DEAD='ff0000'             # Red
SUSPENDED='ff1493'        # Deep pink
TOTAL='c0c0c0'            # Silver

# Taken from ps(1)
# R - Linux, SunOS, FreeBSD, OpenBSD, NetBSD, OSX      (runable)
# S - Linux, SunOS, FreeBSD*, OpenBSD*, NetBSD*, OSX*  (sleeping)
# T - Linux, SunOS, FreeBSD, OpenBSD, NetBSD, OSX      (stopped)
# Z - Linux, SunOS, FreeBSD, OpenBSD, NetBSD, OSX      (zombie)
# D - Linux, FreeBSD, OpenBSD, NetBSD                  (uninterruptible)
# I - FreeBSD, OpenBSD, NetBSD, OSX                    (idle)
# W - Linux*, FreeBSD*                                 (paging/interrupt)
# L - FreeBSD                                          (lock)
# O - SunOS                                            (running)
# X - Linux                                            (dead)
# U - OSX, NetBSD*                                     (uninterruptible/suspended)
# *) Differ meaning

if [ "$1" = "config" ]; then
    echo "graph_title Processes"
    echo "graph_info This graph shows the number of processes"
    echo "graph_category processes"
    echo "graph_args --base 1000 -l 0"

    # OS specific flags
    if [ "$OPERSYS" = "Linux" ]; then
	echo "graph_order sleeping stopped zombie dead paging uninterruptible runnable processes"
	echo "dead.label dead"
	echo "dead.draw STACK"
	echo "dead.colour $DEAD"
	echo "dead.info The number of dead processes."
	echo "paging.label paging"
	echo "paging.draw STACK"
	echo "paging.colour $PAGING"
	echo "paging.info The number of paging processes (<2.6 kernels only)."

    elif [ "$OPERSYS" = "SunOS" ]; then
	echo "graph_order sleeping stopped zombie runnable running total"
	echo "running.label running"
	echo "running.draw STACK"
	echo "running.colour $RUNNING"
	echo "running.info The number of processes that are running on a processor."
	# Be backwards compatible.
	echo "total.label total"
	echo "total.draw LINE1"
        echo "total.colour $TOTAL"
	echo "total.info The total number of processes."

    elif [ "$OPERSYS" = "FreeBSD" ]; then
	echo "graph_order sleeping idle stopped zombie lock uninterruptible interrupt runnable processes"
	echo "lock.label lock"
	echo "lock.draw STACK"
	echo "lock.colour $LOCK"
	echo "lock.info The number of processes that are waiting to acquire a lock."
	echo "interrupt.label interrupt"
	echo "interrupt.draw STACK"
	echo "interrupt.colour $INTERRUPT"
	echo "interrupt.info The number of idle interrupt threads."
      
    elif [ "$OPERSYS" = "OpenBSD" ]; then
	echo "graph_order sleeping idle stopped zombie uninterruptible runnable processes"

    elif [ "$OPERSYS" = "NetBSD" ]; then
	echo "graph_order sleeping idle stopped zombie uninterruptible suspended runnable processes"
	echo "suspended.label suspended"
	echo "suspended.draw STACK"
	echo "suspended.colour $SUSPENDED"
	echo "suspended.info The number of processes that are suspended."

    elif [ "$OPERSYS" = "Darwin" ]; then
	echo "graph_order sleeping idle stopped zombie uninterruptible running processes"
	echo "uninterruptible.label uninterruptible"
	echo "uninterruptible.draw STACK"
	echo "uninterruptible.colour $UNINTERRUPTIBLE"
	echo "uninterruptible.info The number of uninterruptible processes (usually IO)."
    fi

    # Common flags for some OS
    if [ "$OPERSYS" = "FreeBSD" ] || [ "$OPERSYS" = "OpenBSD" ] || 
	[ "$OPERSYS" = "NetBSD" ] || [ "$OPERSYS" = "Darwin" ]; then
	echo "idle.label idle"
	echo "idle.draw STACK"
	echo "idle.colour $IDLE"
	echo "idle.info The number of processes that are idle (sleeping for longer than about 20 seconds)."
	echo "sleeping.label sleeping"
	echo "sleeping.draw AREA"
	echo "sleeping.colour $SLEEPING"
	echo "sleeping.info The number of processes that are sleeping for less than about 20 seconds."
    else
	echo "sleeping.label sleeping"
	echo "sleeping.draw AREA"
	echo "sleeping.colour $SLEEPING"
	echo "sleeping.info The number of sleeping processes."
    fi
   
    if [ "$OPERSYS" = "Linux" ] || [ "$OPERSYS" = "FreeBSD" ] || 
	[ "$OPERSYS" = "OpenBSD" ] || [ "$OPERSYS" = "NetBSD" ]; then
	echo "uninterruptible.label uninterruptible"
	echo "uninterruptible.draw STACK"
	echo "uninterruptible.colour $UNINTERRUPTIBLE"
	echo "uninterruptible.info The number of uninterruptible processes (usually IO)."	
    fi

    # Common flags
    echo "zombie.label zombie"
    echo "zombie.draw STACK"
    echo "zombie.colour $ZOMBIE"
    echo "zombie.info The number of defunct ("zombie") processes (process terminated and parent not waiting)."

    echo "stopped.label stopped"
    echo "stopped.draw STACK"
    echo "stopped.colour $STOPPED"
    echo "stopped.info The number of stopped or traced processes."

    echo "runnable.label runnable"
    echo "runnable.draw STACK"
    echo "runnable.colour $RUNNABLE"
    echo "runnable.info The number of runnable processes (on the run queue)."

    if [ "$OPERSYS" != "SunOS" ]; then
	# Not using 'graph_total' due to backwards compability. SunOS uses 'total'.
        #echo 'graph_total total'
        echo "processes.label total"
        echo "processes.draw LINE1"
        echo "processes.colour $TOTAL"
        echo "processes.info The total number of processes."
    fi

    exit 0
fi

if [ "$OPERSYS" = "Linux" ]; then
    $ps --no-header -eo s | $awk '
{ processes++; stat[$1]++ }
END {
print "processes.value "        0+processes;
print "uninterruptible.value "  0+stat["D"];
print "runnable.value "         0+stat["R"];
print "sleeping.value "         0+stat["S"];
print "stopped.value "          0+stat["T"];
print "paging.value "           0+stat["W"];
print "dead.value "             0+stat["X"];
print "zombie.value "           0+stat["Z"];
}'

elif [ "$OPERSYS" = "SunOS" ]; then
    $ps -e -o s | $awk '
{ total++; stat[$1]++ }
END {
print "total.value "    0+total;
print "running.value "  0+stat["O"];
print "sleeping.value " 0+stat["S"];
print "runnable.value " 0+stat["R"];
print "stopped.value "  0+stat["T"];
print "zombie.value "   0+stat["Z"];
}'
elif [ "$OPERSYS" = "FreeBSD" ]; then
    $ps -axo state= | sed -e 's/^\(.\).*/\1/' | $awk '
{ processes++; stat[$1]++ }
END {
print "processes.value "        0+processes;
print "uninterruptible.value "  0+stat["D"];
print "idle.value "             0+stat["I"];
print "lock.value "             0+stat["G"];
print "runnable.value "         0+stat["R"];
print "sleeping.value "         0+stat["S"];
print "stopped.value "          0+stat["T"];
print "interrupt.value "        0+stat["W"];
print "zombie.value "           0+stat["Z"];
}'
elif [ "$OPERSYS" = "OpenBSD" ]; then
    # First line is header. Remove it.
    $ps -axo state= | sed '1d' | sed -e 's/^\(.\).*/\1/' | $awk '
{ processes++; stat[$1]++ }
END {
print "processes.value "        0+processes;
print "uninterruptible.value "  0+stat["D"];
print "idle.value "             0+stat["I"];
print "runnable.value "         0+stat["R"];
print "sleeping.value "         0+stat["S"];
print "stopped.value "          0+stat["T"];
print "zombie.value "           0+stat["Z"];
}'
elif [ "$OPERSYS" = "NetBSD" ]; then
    # First line is header. Remove it.
    $ps -axo state= | sed '1d' | sed -e 's/^\(.\).*/\1/' | $awk '
{ processes++; stat[$1]++ }
END {
print "processes.value "        0+processes;
print "uninterruptible.value "  0+stat["D"];
print "idle.value "             0+stat["I"];
print "suspended.value "        0+stat["U"];
print "runnable.value "         0+stat["R"];
print "sleeping.value "         0+stat["S"];
print "stopped.value "          0+stat["T"];
print "zombie.value "           0+stat["Z"];
}'

elif [ "$OPERSYS" = "Darwin" ]; then
    # First line is header. Remove it.
    $ps -axo state= | sed '1d' | sed -e 's/^\(.\).*/\1/' | $awk '
{ processes++; stat[$1]++ }
END {
print "processes.value "        0+processes;
print "uninterruptible.value "  0+stat["U"];
print "idle.value "             0+stat["I"];
print "runnable.value "         0+stat["R"];
print "sleeping.value "         0+stat["S"];
print "stopped.value "          0+stat["T"];
print "zombie.value "           0+stat["Z"];
}'
fi

